<template>
  <el-row>
    <edit-modal ref="editModal" @refresh="refresh" :modifyId="modifyId" :routeName="routeName"></edit-modal>
    <el-col :span="24">
      <Portlet>
        <el-form ref="searchForm" class="el-form-col search-form" :model="search">
          <el-row :gutter="15">

            <el-col :lg="4" :md="6">
              <el-form-item prop="workerName">
                <el-input v-model="search.workerName" placeholder="师傅姓名"></el-input>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6">
              <el-form-item prop="phone">
                <el-input v-model="search.phone" placeholder="联系手机"></el-input>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6">
              <el-form-item prop="addressArr">
                <CascadeAddress v-model="search.addressArr" placeholder="服务省市" :level="2" changeOnSelect/>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6" class="hidden-lg-only hidden-xl-only">
              <el-form-item>
                <el-row class="search-btns" :gutter="5">
                  <el-col :span="8">
                    <el-button type="primary" :loading="searchLoading" plain native-type="submit"
                               @click.prevent="searchFunc(search)"><span>搜索</span></el-button>
                  </el-col>
                  <el-col :span="8">
                    <el-button type="info" plain @click="isOpen = !isOpen">{{ isOpen ? '收起' : '展开' }}</el-button>
                  </el-col>
                  <el-col :span="8">
                    <el-button @click="resetFields" plain>重置</el-button>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6">
              <el-form-item prop="businessNo">
                <el-select clearable v-model="search.businessNo" placeholder="业务类型">
                  <el-option v-for="item in businessArr"
                             :key="item.value"
                             :label="item.text"
                             :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6" class="hidden-md-only hidden-sm-only">
              <el-form-item>
                <el-row class="search-btns" :gutter="5">
                  <el-col :span="12">
                    <el-button type="primary" :loading="searchLoading" plain native-type="submit"
                               @click.prevent="searchFunc(search)"><span>搜索</span></el-button>
                  </el-col>
                  <!--<el-col :span="8">-->
                  <!--<el-button type="info" plain @click="isOpen = !isOpen">{{ isOpen ? '收起' : '展开' }}</el-button>-->
                  <!--</el-col>-->
                  <el-col :span="12">
                    <el-button @click="resetFields" plain>重置</el-button>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
          </el-row>

        </el-form>
      </Portlet>
    </el-col>
    <el-col :span="24">
      <BasicTable
        ref="BasicTable"
        :url="tableUrl"
        :tabs="tabs"
        :columns="columns">
        <template slot="buttons">
          <el-button type="primary" @click="add">新增</el-button>
        </template>
        <template slot="column">
          <el-table-column fixed="right" width="100px" align="center" label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="show(scope.row)"
              >
                详情
              </el-button>
              <el-button
                size="mini"
                type="text"
                @click="delRowId(scope.row)"
              >
                移除
              </el-button>
            </template>
          </el-table-column>
        </template>
      </BasicTable>
    </el-col>
  </el-row>
</template>
<script>
import EditModal from '@/views/oms/resource/common/EditModal'
import {unixTimestampFilter} from '@/filters'
import {copy, dotData, confirm} from '@/utils'
import {getInstallBusinessType} from '@/utils/selectOptions'
export default {
  components: {
    EditModal
  },
  props: {
    resourceType: {
      type: String,
      default: ''
    }
  },
  data() {
    let that = this
    let columns = [
      {
        label: '姓名',
        data: 'workerName'
      },
      {
        label: '手机号',
        data: 'phone'
      },
      {
        label: '服务范围',
        data: 'province+city',
        render(data, full) {
          return `${full.province} ${full.city}`
        }
      },
      {
        label: '业务类型',
        data: 'businessName',
        render(data, full) {
          if (that.routeName === 'resourceInstall') {
            return `${full.business}`
          } else {
            return full.businessName
          }
        }
      },
      {
        label: '结算方式',
        data: 'clearingFormName'
      },
      {
        label: '结算比例(%)',
        data: 'clearingRatio'
      },
      {
        label: '基本工资',
        data: 'basePay'
      },
      {
        label: '添加时间',
        data: 'foundTime',
        width: '134px',
        render(data, full) {
          return unixTimestampFilter(data)
        }
      }
    ]
    return {
      routeName: this.$route.name,
      businessArr: [],
      modifyId: '',
      isOpen: false,
      visible: false,
      searchLoading: false,
      search: {
        addressArr: [],
        workerName: '',
        phone: '',
        businessNo: ''
      },
      columns,
      tabs: []
    }
  },
  created() {
    getInstallBusinessType().then(arr => {
      this.businessArr = arr
    })
  },
  computed: {
    tableUrl() {
      let url = ''
      if (this.routeName === 'resourceDistribution') {
        url = '/ms-warehouse-order/resourceManage/selectDelivers'
      } else if (this.routeName === 'resourceInstall') {
        url = '/ms-warehouse-order/resourceManage/selectInstalls'
      } else if (this.routeName === 'resourcePickup') {
        url = '/ms-warehouse-order/resourceManage/selectPickups'
      } else if (this.routeName === 'resourceSecondary') {
        url = '/ms-warehouse-order/resourceManage/selectSecondary'
      } else if (this.routeName === 'resourceDistributionInstall') {
        url = '/ms-warehouse-order/resourceManage/selectDeliversAndInstall'
      }
      return url
    }
  },
  methods: {
    // 搜索
    searchFunc(search) {
      const basicTable = this.$refs.BasicTable
      if (basicTable) {
        let params = copy(search)
        params.provinceNo = dotData(params, 'addressArr[0]') || ''
        params.cityNo = dotData(params, 'addressArr[1]') || ''
        delete params.addressArr
        this.searchLoading = true
        basicTable.search(params).finally(_ => {
          this.searchLoading = false
        })
      }
    },
    // 重置表单
    resetFields() {
      const searchForm = this.$refs.searchForm
      if (searchForm) {
        searchForm.resetFields()
      }
    },
    // 表格刷新
    refresh() {
      this.searchFunc(this.search)
    },
    add() {
      this.modifyId = ''
      this.$refs.editModal.show()
    },
    show(data) {
      this.$router.push({path: '/ss-oms/resource/common/show/' + data.id, query: {resourceType: this.resourceType}})
    },
    // 删除
    delRowId(rowId) {
      let url = ''
      if (this.routeName === 'resourceDistribution') {
        url = '/ms-warehouse-order/resourceManage/operateDeliver'
      } else if (this.routeName === 'resourceInstall') {
        url = '/ms-warehouse-order/resourceManage/operateInstall'
      } else if (this.routeName === 'resourcePickup') {
        url = '/ms-warehouse-order/resourceManage/operatePickup'
      } else if (this.routeName === 'resourceSecondary') {
        url = '/ms-warehouse-order/resourceManage/operateSecondary'
      } else if (this.routeName === 'resourceDistributionInstall') {
        url = '/ms-warehouse-order/resourceManage/operateDeliverAndInstall'
      }
      let postData = {
        id: rowId.id,
        operation: 'delete'
      }
      confirm('你确定要从合作师傅中移除该师傅吗？', '温馨提示', {
        confirmButtonLoading: true,
        type: 'warning',
        center: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmCallBack: ({confirmButtonLoadingClose, close}) => {
          return this.$ajax.post(url, postData).then(response => {
            if (response.success) {
              close().then(_ => {
                this.$message({
                  type: 'success',
                  message: '成功移除!'
                })
                this.refresh()
              })
            } else {
              confirmButtonLoadingClose()
            }
          }).finally(_ => {
            confirmButtonLoadingClose()
          })
        }
      })
    }
  }
}
</script>
